<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="menuList"
      default-expand-all
      row-key="menuId"
      size="small"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="title" label="菜单名称" :show-overflow-tooltip="true" min-width="180" />
      <el-table-column prop="path" label="路由地址" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column prop="routerName" label="路由名称" :show-overflow-tooltip="true" min-width="140" />
      <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true" min-width="240" />
      <el-table-column prop="icon" label="图标" align="center" min-width="80">
        <template slot-scope="scope">
          <i :class="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column prop="index" label="排序" min-width="60" />
      <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      top="10vh"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" style="width: 430px;">
        <el-form-item label="父级菜单" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :options="menuOptions"
            :normalizer="normalizer"
            style="font-size: 13px;"
            placeholder="选择父级菜单"
          />
        </el-form-item>
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="form.icon" placeholder="输入饿了么图标" />
        </el-form-item>
        <el-form-item label="路由地址" prop="path">
          <el-input v-model="form.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item label="路由名称" prop="routerName">
          <el-input v-model="form.routerName" placeholder="请输入路由名称" />
        </el-form-item>
        <el-form-item label="组件路径" prop="component">
          <el-input v-model="form.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item label="权限标识" prop="perms">
          <el-input v-model="form.perms" placeholder="请权限标识" maxlength="50" />
        </el-form-item>
        <el-form-item label="隐藏菜单" prop="hidden">
          <el-switch v-model="form.hidden" active-text="显示" inactive-text="隐藏" />
        </el-form-item>
        <el-form-item label="缓存" prop="cache">
          <el-switch v-model="form.cache" active-text="开启" inactive-text="关闭" />
        </el-form-item>
        <el-form-item label="排序" prop="index">
          <el-input-number v-model="form.index" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sysAjax } from 'Ajax'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: '',
  components: { Treeselect },
  data() {
    return {
      loading: true, // 遮罩层
      menuList: [], // 菜单表格树数据
      menuOptions: [], // 菜单树选项
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        cache: true,
        hidden: false,
        index: 1
      },
      // 表单校验
      rules: {
        parentId: [{ required: true, message: '父级菜单不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
        path: [{ required: true, message: '路由地址不能为空', trigger: 'blur' }],
        index: [{ required: true, message: '菜单顺序不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.icon = name
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true
      sysAjax.allMenuTree().then(response => {
        this.menuList = response
        this.loading = false
        this.menuOptions = [{ menuId: 0, title: '根目录', children: response }]
      })
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if ((!node.children || !node.children.length)) {
        delete node.children
      }
      return {
        id: node.menuId,
        label: node.title,
        children: node.children
      }
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.form = {
        cache: true,
        hidden: false,
        index: 1
      }
      if (row != null && row.menuId) {
        this.form.parentId = row.menuId
      } else {
        this.form.parentId = 0
      }
      this.open = true
      this.title = '添加菜单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row
      this.open = true
      this.title = '修改菜单'
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          console.log('form', this.form)
          console.log('component', this.form.component)
          if (this.form.component !== undefined || this.form.routerName !== undefined) {
            const bool = this.form.component !== undefined && this.form.routerName !== undefined
            if (!bool) {
              this.msgError('若填入的是最后一级菜单，请确保【组件路径】与【路由名称】都正确录入，否则路由访问将异常')
              return
            }
          }
          if (this.form.menuId !== undefined) {
            sysAjax.updateMenu(this.form).then(() => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            sysAjax.addMenu(this.form).then(() => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除菜单【' + row.title + '】?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return sysAjax.delMenu({ 'menuId': row.menuId }).then((res) => {
          console.log(res, '返回结果')
        })
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
<style scoped>
/deep/ .el-form-item__label {
  font-size: 13px;
  font-weight: 500;
}
/deep/ .el-switch__label * {
  font-size: 13px;
}
</style>
